package cn.com.twoke.easyproject.common.utils;

import cn.hutool.core.util.StrUtil;

public class Base62Encoder {
    private static final String BASE62 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String encode(Long num) {
        StringBuilder sb = new StringBuilder();
        while (num > 0) {
            sb.insert(0, BASE62.charAt((int) (num % 62)));
            num /= 62;
        }
        return sb.toString();
    }

    public static Long decode(String str) {
        if (StrUtil.isBlank(str)) return null;
        long num = 0;
        for (int i = 0; i < str.length(); i++) {
            num = num * 62 + BASE62.indexOf(str.charAt(i));
        }
        return num;
    }
}